<template>
  <el-drawer :visible.sync="drawer" direction="rtl" size="45%" :with-header="false" :width="700">
    <div class="drawer_box">
      <div class="drawer_t">
        <div class="drawer_t_l">
          <img class="drawer_t_l_img" src="/static/skin/default/MCS/AIGC/Inventory/images/add.png" alt="" />
          更新库存
        </div>
        <img class="drawer_t_r" src="/static/skin/herodoo/main/platform/home/img/guanbi.png" alt=""
          @click="drawer = false" />
      </div>
      <div class="drawer_c">
        <div class="list">
          <div class="list_right"><span>权益名称:</span></div>
          <div class="list_left">
            <div>{{ messageList.Name }}</div>
          </div>
        </div>
        <div class="list">
          <div class="list_right"><span>权益剩余库存:</span></div>
          <div class="list_left">
            <div>{{ messageList.InventoryQuantity }}份</div>
          </div>
        </div>
        <div class="list">
          <div class="list_right"><span>库存数量:</span></div>
          <div class="list_left">
            <input type="text" v-model="num" oninput="value=value.replace(/[^\d]/g, '')" placeholder="请输入更新的库存数量" />
          </div>
        </div>
      </div>
      <div class="cancel_box" @click="save">
        更新库存
      </div>
    </div>
  </el-drawer>
</template>

<script>
export default {
  name: "editDialog",
  data() {
    return {
      octopus: this.$parent.octopus,
      drawer: false,
      num: null
    };
  },
  props: {
    messageList: {
      type: Object,
      default: {}
    }
  },

  computed: {},
  methods: {
    drawerOp() {
      this.drawer = true;
      this.num = '';
    },
    save() {
      if (!this.num) {
        this.$message({
          type: "error",
          message: "请输入更新的库存数量"
        });
        return;
      }

      if (this.num > 10000) {
        this.$message({
          type: "error",
          message: "最大库存为1万。"
        });
        return;
      }
      this.$confirm("确定更新该权益库存吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(res => {
        if (res == "confirm") {
          this.updateInventory();
        }
      });
    },
    updateInventory() {
      const o = this;



      if (this.messageList.TypeID == 2) {
        this.api_cmbEquity();
        return;
      }

      const channelID = this.messageList.TypeID == 2 ? 3 : null; // 是否为权益礼包
      this.octopus.tools.openAPI(
        "/ESB/AIGC/CreateEquityRecord",
        {
          companyID: this.messageList.CompanyID, // 权益所属公司ID
          equityID: this.messageList.ID, // 权益ID
          quantity: this.num, // 单个权益数量
          channelID: channelID, // 权益类型
          createUserID: this.octopus.passport.id // 创建人
        },
        function (data) {
          if (data.status.code == 200) {
            o.$message.success("库存更新成功");
            o.$emit("reload");
            o.drawer = false;
          } else {
            o.octopus.tips.error(data.status.message);
            o.drawer = false;
          }
        }
      );

      // this.octopus.request.api.post("/ESB/Services/Tools/Data/Get", {
      //     requestType: "POST_JSON", // POST_JSON
      //     url: "http://wxzf001.natapp1.cc/ESB/AIGC/CreateEquityRecord",
      //     params: {
      //         "companyID": this.messageList.CompanyID, // 权益所属公司ID
      //         "equityID": this.messageList.ID, // 权益ID
      //         "quantity": this.num,// 单个权益数量
      //         "createUserID": this.octopus.passport.id,// 创建人
      //         "channelID": channelID, // 权益类型
      //     },
      // }, function (apiData) {
      //     if (apiData.content.response.status.code == 200) {
      //         o.$message.success('库存更新成功')
      //         o.drawer = false
      //     } else {
      //         o.octopus.tips.error(apiData.content.response.content)
      //     }
      // });
    },
    // 组合权益插入库存
    api_cmbEquity() {
      this.octopus.tools.openAPI(
        "/ESB/AIGC/cmbEquity",
        {
          userId: this.octopus.passport.id,
          equityId: this.messageList.ID, //组合权益ID
          typeId: 2, //
          companyId: this.messageList.CompanyID, // 公司ID
          quantity: this.num, //组合权益数量
          baseEquityList: [], // 组合权益的子权益
          channelId: "3" // 页面不同 创建页面传1 ，权益审核传2，库存管理传3
        },
        datas => {
          if (datas.content.status.code == 200) {
            this.$emit("reload");
            this.drawer = false;
            this.$message({
              message: "创建成功 ",
              type: "success"
            });
          } else {
            this.octopus.tips.error(datas.content.content);
          }
        }
      );
    }
  }
};
</script>
<style scoped>
.drawer_box {
  padding: 3rem 0.8rem 3rem 0;
  height: calc(100% - 5rem);
  display: flex;
  flex-direction: column;
}

.drawer_t {
  padding: 0 2.8rem;
  display: flex;
  justify-content: space-between;
}

.drawer_t_l {
  display: flex;
  align-items: center;
  font-size: 1.8rem;
  font-weight: bold;
}

.drawer_t_l_img {
  width: 2.8rem;
  margin-right: 1rem;
}

.drawer_t_r {
  width: 3rem;
  height: 3rem;
  cursor: pointer;
}

.cancel_box {
  line-height: 4rem;
  background-color: #f78c51;
  color: #fff;
  position: absolute;
  padding: 0.2rem 3rem;
  border-radius: 0.5rem;
  bottom: 1rem;
  right: 2rem;
  cursor: pointer;
  box-shadow: inset 0px 0px 2px 0px #ffffff;
}

.drawer_c {
  width: calc(100% - 6rem);
  margin: auto;
  margin-top: 3rem;
  background-color: #f8f4f3;
  box-shadow: inset 0px 0px 4px 0px #e9e9e9;
  border-radius: 1.5rem;
  padding: 2rem;
  box-sizing: border-box;
}

.drawer_c .list {
  display: flex;
  justify-content: space-between;
  font-size: 1.8rem;
  line-height: 5rem;
  margin-top: 1.5rem;
}

.drawer_c .list .list_right {
  width: 30%;
}

.drawer_c .list .list_left {
  width: 70%;
  color: #a3a3a3;
}

.drawer_c .list .list_left input {
  width: 100%;
  padding: 0 2rem;
  border: 1px solid #f3e8e2;
  border-radius: 1rem;
}

.drawer_c .list .list_left>div {
  padding: 0 2rem;
  border-radius: 1rem;
  background-color: #ececec;
}
</style>
